package com.liuxing.doctor.controller;

import com.liuxing.doctor.common.Object.ReturnObject;
import com.liuxing.doctor.entity.Doctor;
import com.liuxing.doctor.service.DoctorService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @athuer 行柳
 */
@Controller
public class UserController {
    @Resource
    private DoctorService doctorService;
    @RequestMapping(value = "/changePassword",method = RequestMethod.GET)
    public String changePassword(HttpServletRequest request, Model model){
        Doctor doctor= (Doctor) request.getSession().getAttribute("doctor");
        model.addAttribute("doctor",doctor);
        return "/user/change_password";
    }
    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    public Object changePassword(String password_new,HttpServletRequest request,Model model){
        Doctor doctor= (Doctor) request.getSession().getAttribute("doctor");
        Map<String,Object> map=new HashMap<>();
        map.put("password",password_new);
        map.put("username",doctor.getDoctorId());
        int resCode=doctorService.updateDoctorLoginPasswd(map);
        ReturnObject returnObject=new ReturnObject();
        if(resCode==1){
            returnObject.setCode("1");
        }else{
            returnObject.setMessage("网络错误,无法连接服务器");
        }
        return returnObject;
    }
    @RequestMapping("/logout")
    public String logout(){
        return "index";
    }
    @RequestMapping("/changeAccount")
    public String changeAccount(){
        return "index";
    }
}
